import json
import os
from config import BASE_PATH

"""
    封装一些公共方法
"""


# 读取json文件
def read_file(file_name, need_key):
    filePath = BASE_PATH + os.sep + 'data' + os.sep + file_name
    with open(filePath, mode='r', encoding='utf-8') as f:
        json_data = json.load(f)
        # print(json_data) # => [[],[],[],[]] 或者 ["","",""]
        list_data = []
        # 获取列表
        value = json_data.get(need_key)
        # 遍历列表，数据转元组
        for data in value:
            list_data.append([data])
            # list_data.append(data)
        return list_data


# 写入json
def write_json(filename, data):
    filepath = BASE_PATH + os.sep + "data" + os.sep + filename
    with open(filepath, "w", encoding="utf-8")as f:
        json.dump(data, f, ensure_ascii=False, indent=2)

# 字符串截取-用于需要打印长字符串
def str_limit(val,length):
    suffix = "..."
    if length >= len(val):
        result = val
    else:
        result = val[:length] + suffix
    return result

if __name__ == "__main__":
    pass
    # print(str_limit("11111112321333333333",8))
    # print(read_json("parsing.json"))
    # print(read_json("sniffing.json","accessibility"))